<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Horse races' );
?>
					<div class="blok">
<?php
printDiplomacyTab(); 
?>
						</div> 
						<br />
<?php
$race_id = -1;
if( isset( $_GET[ 'race_id' ] ) ) {
	$race_id = formatInput( $_GET[ 'race_id' ] );
}
if( is_numeric( $race_id ) ) {
	$query = "SELECT * FROM races WHERE race_id = $race_id";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'race_id' ] ) ) {
		$current_turn = getCurrentTurn();
		$prizes = $row[ 'prizes' ];
		$date = $row[ 'date' ];
		$calc_turn = $turn = $row[ 'turn' ];
		
		$query2 = "SELECT SUM(gold) FROM race_bets WHERE race_id = $race_id";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$totalGold = $row2[ 'SUM(gold)' ];
?>		
				<div class="blok">
<?php
		if( isset( $date ) && $date != '' ) {
			$message = translate( "You did not bet on this race." );
			$turn = $current_turn - $turn;
			if( $turn == 1 ) {
				$turn = " (" . translate( '1 day ago' ) . ")";
			}
			else if( $turn == 0 ) {
				$turn = "";
			}
			else {
				$turn = " ($turn " . translate( 'days ago' ) . ")";
			}
?>
					<?php transprint( 'Date' ); ?>: <?php print( $date ); ?><?php print( $turn ); ?><br /><br />
					<?php transprint( 'Total betting gold' ); ?>: <strong class="gold"><?php print( number_format( $totalGold ) ); ?></strong><br />
					<?php transprint( 'Total prize gold' ); ?>: <strong class="gold"><?php print( number_format( $prizes ) ); ?></strong><br />
<?php
			$query = "SELECT race_history.race_team_id, race_teams.name FROM race_history JOIN race_teams ON race_history.race_team_id = race_teams.race_team_id WHERE race_id = $race_id ORDER BY rank ASC LIMIT 0, 3";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$num = mysql_numrows( $result );
			$i = 0;
			while( $num > $i ) {
				if( $i == 0 ) {
					$first = "<a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>";
				}
				else if( $i == 1 ) {
					$second = "<a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>";
				}
				else if( $i == 2 ) {
					$third = "<a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>";
				}
				$i ++;
			}
			
			print( translate( '1st' ) . ": $first (" . translate( 'Prize' ) . ": <strong class=\"gold\">" . number_format( round( $prizes * 0.6 ) ) . "</strong>)<br />\r\n" );
			print( translate( '2nd' ) . ": $second (" . translate( 'Prize' ) . ": <strong class=\"gold\">" . number_format( round( $prizes * 0.3 ) ) . "</strong>)<br />\r\n" );
			if( isset( $third ) ) {
				print( translate( '3rd' ) . ": $third (" . translate( 'Prize' ) . ": <strong class=\"gold\">" . number_format( round( $prizes * 0.1 ) ) . "</strong>)<br /><br />\r\n" );
			}
		}
		else {
			$message = translate( "You have not placed a bet on this race yet." );
			$turn = $turn - $current_turn;
			if( $turn == 1 ) {
				$turn = translate( "1 day" );
			}
			else {
				$turn = "$turn " . translate( 'days ' );
			}
?>
					<?php transprint( 'Race in' ) ?> <?php print( $turn ); ?><br /><br />
					<?php transprint( 'Total prize gold' ) ?>: <strong class="gold"><?php print( number_format( $prizes ) ); ?></strong><br />
					<?php transprint( 'Total betting gold' ) ?>: <strong class="gold"><?php print( number_format( $totalGold ) ); ?></strong><br />
<?php
			print( translate( '1st place reward' ) . ": <strong class=\"gold\">" . number_format( round( $prizes * 0.6 ) ) . "</strong><br />\r\n" );
			print( translate( '2nd place reward' ) . ": <strong class=\"gold\">" . number_format( round( $prizes * 0.3 ) ) . "</strong><br />\r\n" );
			print( translate( '3rd place reward' ) . ": <strong class=\"gold\">" . number_format( round( $prizes * 0.1 ) ) . "</strong><br />\r\n" );
		}
?>
				</div> <!-- close blok -->
				<br />
<?php
		$query = "SELECT race_bets.*, race_teams.name FROM race_bets JOIN race_teams ON race_bets.race_team_id = race_teams.race_team_id WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND race_id = $race_id ORDER BY gold DESC";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$num = mysql_numrows( $result );
		if( $num > 0 ) {
		    print "<table id=\"twothird\"><th colspan=\"3\">" . translate( 'My bets' ) . "</th>\r\n";
		    print "<tr><td>" . translate( 'Team' ) . "</td><td>" . translate( 'Gold' ) . "</td><td>" . translate( 'Payout' ) . "</td></tr>\r\n";
		    $i = 0;
		    while ( $i < $num ) {
		        if( $i % 2 == 0 ) {
		            print "<tr class=\"even\">";
		        }
		        else {
		            print "<tr>";
		        }
		    	
		    	print( "<td><a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a></td>" );
		    	print( "<td align=\"right\"><strong class=\"gold\">" . number_format( mysql_result( $result, $i, "gold" ) ) . "</strong></td>" );
			 	
			 	$query2 = "SELECT count(*) " .
			 			"FROM race_history " .
			 			"JOIN races ON races.race_id = race_history.race_id " .
			 			"WHERE turn < $calc_turn AND race_team_id = " . mysql_result( $result, $i, "race_team_id" );
				$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$races = $row2[ 'count(*)' ];
				$won = 0;
				
				if( $races > 0 ) {
					$query2 = "SELECT count(*) " .
							"FROM race_history " .
				 			"JOIN races ON races.race_id = race_history.race_id " .
							"WHERE turn < $calc_turn AND rank = 1 AND race_team_id = " . mysql_result( $result, $i, "race_team_id" );
					$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
					$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
					$won = $row2[ 'count(*)' ];
					
					//print( "avg_win: $avg_win <br />" );
          $odds = pow(1.1, $races/max(1, $won));
          
					if( $won == 0 ) {
						$avg_win =  '0%';
					}
					else {
						$avg_win =  round(($won / $races) * 100.0, 2 ) . '%';
					}
				}
				else {
					$odds = 2;
					$avg_win = "-";
				}
				
				$odds = round( $odds, 2 ) . ":1";
				
	    	print( "<td align=\"right\">$odds</td></tr>\r\n" );
	    	$i ++;
	    }
		}
		else {
			?>
						<blok class="blok">
							<?php print( $message ); ?>
						</blok>
			<?php
		}
	}
	else {
		transprint( "No valid race id..." );
	}
}
else {
	transprint( "No valid race id..." );
}
aos_footer();
?>